<template>
  <view class="my-house-container">
    <!-- 加载状态 -->
    <view v-if="loading" class="loading-container">
      <u-loading-icon mode="circle" size="40" color="#21d59d"></u-loading-icon>
      <text class="loading-text">加载中...</text>
    </view>

    <!-- 房屋列表 -->
    <view v-else-if="houseList.length > 0" class="house-list">
      <view 
        v-for="house in houseList" 
        :key="house.id"
        class="house-card"
        @click="viewHouseDetail(house)"
      >
        <view class="house-header">
          <view class="house-number">
            <text class="number-text">{{ house.number }}号</text>
            <text class="orientation">{{ house.orientation }}</text>
          </view>
          <view class="house-status" :class="{ 'rented': house.renterId }">
            <text class="status-text">{{ house.renterId ? '租用' : '自住' }}</text>
          </view>
        </view>

        <view class="house-info">
          <view class="info-row">
            <view class="info-item">
              <text class="label">建筑面积</text>
              <text class="value">{{ house.floorSpace }}㎡</text>
            </view>
            <view class="info-item">
              <text class="label">套内面积</text>
              <text class="value">{{ house.insideSpace }}㎡</text>
            </view>
          </view>
          
          <view class="info-row">
            <view class="info-item">
              <text class="label">户型</text>
              <text class="value">{{ house.room }}室{{ house.hall }}厅{{ house.toilet }}卫</text>
            </view>
            <view class="info-item">
              <text class="label">公摊面积</text>
              <text class="value">{{ house.sharedSpace }}㎡</text>
            </view>
          </view>
        </view>

        <view class="house-footer">
          <text class="create-time">创建时间：{{ formatTime(house.createTime) }}</text>
          <view class="arrow-icon">
            <u-icon name="arrow-right" size="16" color="#ccc"></u-icon>
          </view>
        </view>
      </view>
    </view>

    <!-- 空状态 -->
    <view v-else class="empty-container">
      <view class="empty-icon">🏠</view>
      <text class="empty-text">暂无房屋信息</text>
      <text class="empty-desc">请联系物业管理员添加房屋信息</text>
    </view>
  </view>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue';
import { HouseApi } from '@/api';
import type { HouseInfo } from '@/api/house/types';
import { getToken } from '@/utils/auth';
import { getUserIdFromToken } from '@/utils/auth/jwt';

// 响应式数据
const loading = ref(true);
const houseList = ref<HouseInfo[]>([]);

// 获取房屋列表
async function fetchHouseList() {
  try {
    loading.value = true;
    
    // 从JWT token中获取用户ID
    const token = getToken();
    const ownerId = getUserIdFromToken(token);
    
    if (!ownerId) {
      uni.showToast({
        title: '无法获取用户信息，请重新登录',
        icon: 'none'
      });
      return;
    }
    
    const response = await HouseApi.getHouseByOwnerId({ ownerId });
    
    if (response.code === 0) {
      houseList.value = response.data;
    } else {
      uni.showToast({
        title: response.message || '获取房屋信息失败',
        icon: 'none'
      });
    }
  } catch (error) {
    console.error('获取房屋信息失败:', error);
    uni.showToast({
      title: '网络错误，请重试',
      icon: 'none'
    });
  } finally {
    loading.value = false;
  }
}

// 查看房屋详情
function viewHouseDetail(house: HouseInfo) {
  console.log('查看房屋详情:', house);
  uni.navigateTo({
    url: `/pages/common/house-detail/index?houseId=${house.id}`
  });
}

// 格式化时间
function formatTime(timeStr: string) {
  const date = new Date(timeStr);
  return date.toLocaleDateString('zh-CN', {
    year: 'numeric',
    month: '2-digit',
    day: '2-digit'
  });
}

// 页面加载时获取数据
onMounted(() => {
  fetchHouseList();
});
</script>

<style lang="scss" scoped>
.my-house-container {
  min-height: 100vh;
  background: #f5f5f5;
  padding: 20rpx;
}

.loading-container {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 400rpx;
}

.loading-text {
  margin-top: 20rpx;
  font-size: 28rpx;
  color: #666;
}

.house-list {
  display: flex;
  flex-direction: column;
  gap: 20rpx;
}

.house-card {
  background: white;
  border-radius: 16rpx;
  padding: 30rpx;
  box-shadow: 0 2rpx 12rpx rgba(0, 0, 0, 0.1);
  transition: all 0.3s ease;
}

.house-card:active {
  transform: scale(0.98);
  box-shadow: 0 1rpx 6rpx rgba(0, 0, 0, 0.15);
}

.house-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20rpx;
}

.house-number {
  display: flex;
  align-items: center;
  gap: 16rpx;
}

.number-text {
  font-size: 36rpx;
  font-weight: bold;
  color: #333;
}

.orientation {
  font-size: 24rpx;
  color: #666;
  background: #f0f0f0;
  padding: 8rpx 16rpx;
  border-radius: 20rpx;
}

.house-status {
  background: #21d59d;
  color: white;
  padding: 8rpx 16rpx;
  border-radius: 20rpx;
  font-size: 24rpx;
}

.house-status.rented {
  background: #ff9800;
}

.house-info {
  margin-bottom: 20rpx;
}

.info-row {
  display: flex;
  justify-content: space-between;
  margin-bottom: 16rpx;
}

.info-item {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
}

.label {
  font-size: 24rpx;
  color: #666;
  margin-bottom: 8rpx;
}

.value {
  font-size: 28rpx;
  color: #333;
  font-weight: 500;
}

.house-footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding-top: 20rpx;
  border-top: 1rpx solid #f0f0f0;
}

.create-time {
  font-size: 24rpx;
  color: #999;
}

.arrow-icon {
  display: flex;
  align-items: center;
  justify-content: center;
}

.empty-container {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 500rpx;
  padding: 40rpx;
}

.empty-icon {
  font-size: 120rpx;
  margin-bottom: 30rpx;
}

.empty-text {
  font-size: 32rpx;
  color: #333;
  margin-bottom: 16rpx;
}

.empty-desc {
  font-size: 28rpx;
  color: #666;
  text-align: center;
  line-height: 1.5;
}
</style>
